import dash
from dash import html, dcc
import feffery_antd_components as fac
from dash.dependencies import Input, Output
# 使用flask中的诸多实用功能
from flask import request

from server import app

app.layout = html.Div(
    [
        dcc.Location(
            id='url'
        ),
        html.Div(
            id='page-content'
        )
    ],
    style={
        'padding': '50px 100px'
    }
)


@app.callback(
    Output('page-content', 'children'),
    Input('url', 'pathname')
)
def render_content(pathname):
    print(request.remote_addr)

    print(request.cookies.get("first_visit_in_10s"))

    # 检查是否存在相应cookies
    if not request.cookies.get("first_visit_in_10s"):
        # 否则设置有效期为10秒的cookies
        dash.callback_context.response.set_cookie('first_visit_in_10s', 'yes', max_age=10)

        return [
            fac.AntdMessage(
                content='欢迎访问！',
                type='info'
            ),
            fac.AntdSegmented(
                options=[
                    {
                        'label': f'选项{i}',
                        'value': f'选项{i}'
                    }
                    for i in range(5)
                ],
                id='input-demo',
                defaultValue='选项1',
                block=True
            ),
            html.Div(
                id='output-demo'
            )
        ]

    return [
        fac.AntdSegmented(
            options=[
                {
                    'label': f'选项{i}',
                    'value': f'选项{i}'
                }
                for i in range(5)
            ],
            id='input-demo',
            defaultValue='选项1',
            block=True
        ),
        html.Div(
            id='output-demo'
        )
    ]


@app.callback(
    Output('output-demo', 'children'),
    Input('input-demo', 'value')
)
def callback_demo(value):
    return value


if __name__ == '__main__':
    app.run_server(debug=True)
